package cn.codeforfun.common;

import cn.codeforfun.interceptor.adapter.CallbackInterceptorAdaptor;
import cn.codeforfun.interceptor.adapter.LoginInterceptorAdaptor;
import cn.codeforfun.interceptor.adapter.OauthInterceptorAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.*;

/**
 * Created by Administrator on 2017/2/14 0014.
 */
@Configuration
public class MvcConfigurer extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/error").setViewName("error.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        super.configurePathMatch(configurer);
        configurer.setUseSuffixPatternMatch(false);
    }

    @Bean
    public OauthInterceptorAdapter getOauthInterceptorAdapter() {
        return new OauthInterceptorAdapter();
    }

    @Bean
    public LoginInterceptorAdaptor getLoginInterceptorAdaptor() {
        return new LoginInterceptorAdaptor();
    }

    @Bean
    public CallbackInterceptorAdaptor getCallbackInterceptorAdaptor() {
        return new CallbackInterceptorAdaptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getLoginInterceptorAdaptor()).addPathPatterns("/**").excludePathPatterns("/callback", "/material");
        registry.addInterceptor(getCallbackInterceptorAdaptor()).addPathPatterns("/callback");
        registry.addInterceptor(getOauthInterceptorAdapter()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}
